﻿// Copyright 2001-2010 - Active Up SPRLU (http://www.agilecomponents.com)
//
// Data Connection Suite is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.

// Data Connection Suite is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
// for more details.

// You should have received a copy of the GNU General Public License along with
// Data Connection Suite. If not, see http://www.gnu.org/licenses/.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;

namespace AgileComponents.DataConnection
{
    /// <summary>
    /// 
    /// </summary>
    public class DataConnectionControl : UserControl
    {
        /// <summary>
        /// Tests the connection string.
        /// </summary>
        /// <param name="serverName">Name of the server.</param>
        public void TestConnectionString(string serverName)
        {
            if (string.IsNullOrEmpty(serverName))
            {
                MessageBox.Show("This connection cannot be tested because no server name has been specified.", "Connection String Builder", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            try
            {
                using (IDbConnection connection = this.GetConnection())
                {
                    connection.Open();
                }

                MessageBox.Show("Test connection succeeded.", "Connection String Builder", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection String Builder", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Gets the connection string.
        /// </summary>
        /// <returns></returns>
        public virtual IDbConnection GetConnection()
        {
            return null;
        }

        private DbConnectionStringBuilder _builder;
        /// <summary>
        /// Gets the builder.
        /// </summary>
        /// <value>The builder.</value>
        public DbConnectionStringBuilder Builder
        {
            get
            {
                return _builder;
            }
            set
            {
                _builder = value;
            }
        }

        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public virtual string ConnectionString
        {
            get
            {
                if (_builder != null)
                    return _builder.ToString();
                else
                    return null;
            }
            set
            {
                if (_builder == null)
                    throw new Exception("Can't set connection string. Builder property not set.");

                _builder.ConnectionString = value;
            }
        }

        #region Protected Properties

        private bool _databaseListCreated;
        /// <summary>
        /// Gets or sets a value indicating whether [database list created].
        /// </summary>
        /// <value><c>true</c> if [database list created]; otherwise, <c>false</c>.</value>
        protected bool DatabaseListCreated
        {
            get
            {
                return _databaseListCreated;
            }
            set
            {
                _databaseListCreated = value;
            }
        }

        private bool _databaseListCreating;
        /// <summary>
        /// Gets or sets a value indicating whether [database list creating].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [database list creating]; otherwise, <c>false</c>.
        /// </value>
        protected bool DatabaseListCreating
        {
            get
            {
                return _databaseListCreating;
            }
            set
            {
                _databaseListCreating = value;
            }
        }

        #endregion

        #region Events

        public delegate void DataConnectionChangedEventHandler(object sender, EventArgs e);

        // Declare the event.
        public event DataConnectionChangedEventHandler DataConnectionChanged;

        // Wrap the event in a protected virtual method
        // to enable derived classes to raise the event.
        protected virtual void RaiseDataConnectionChanged()
        {
            // Raise the event by using the () operator.
            if (DataConnectionChanged != null)
                DataConnectionChanged(this, null);
        }

        #endregion
    }
}
